﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageDisputeCategories : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_DISPUTECATEGORY = "__DisputeCategory::DC";
    private const string PROP_LAYOUT = "__DisputeCategory::UIL";
    private const string PROP_SELECTED_DISPUTECATEGORY_ID = "__DisputeCategory::SDCID";

    #endregion

    #region Public Properties

    public DisputeCategoryCollection DataSourceDisputeCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] is DisputeCategoryCollection ? (DisputeCategoryCollection)SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] : (DisputeCategoryCollection)(SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] = new DisputeCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] = value; }
    }

    public int SelectedDisputeCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_DISPUTECATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_DISPUTECATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_DISPUTECATEGORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceDisputeCategory = DisputeCategory.GetAllDisputeCategories();
            DisputeCategoryList1.DataBind(DataSourceDisputeCategory);
        }
    }

    #endregion

    #region Control: DisputeCategoryList

    protected void ucDisputeCategoryList_EditRequested(object sender, Controls_DisputeCategoryList.EditRequestedEventArgs evt)
    {
        SelectedDisputeCategoryID = evt.DC.DisputeCategoryID;
        DisputeCategoryViewEdit1.DataBind(evt.DC);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucDisputeCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        DisputeCategoryViewEdit1.CancelEdit();
        DisputeCategory newDisputeCategory = DisputeCategory.CreateNew(DataSourceDisputeCategory);
        SelectedDisputeCategoryID = newDisputeCategory.DisputeCategoryID;
        DisputeCategoryViewEdit1.DataBind(DataSourceDisputeCategory.FindByDisputeCategoryID(SelectedDisputeCategoryID));
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucDisputeCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceDisputeCategory = DisputeCategory.GetAllDisputeCategories();
        DisputeCategoryList1.DataSource = DataSourceDisputeCategory;
    }

    #endregion

    #region Control: DisputeCategoryViewEdit

    protected void ucDisputeCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        DisputeCategoryViewEdit1.DataSourceDisputeCategory = DataSourceDisputeCategory.FindByDisputeCategoryID(SelectedDisputeCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        DisputeCategoryViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceDisputeCategory.FindByDisputeCategoryID(SelectedDisputeCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceDisputeCategory.Remove(DataSourceDisputeCategory.FindByDisputeCategoryID(SelectedDisputeCategoryID));
                SelectedDisputeCategoryID = (DataSourceDisputeCategory[0] != null ? DataSourceDisputeCategory[0].DisputeCategoryID : 0);
            }
            catch
            {
            }
        }
        DisputeCategoryList1.DataBind(DataSourceDisputeCategory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (DisputeCategoryViewEdit1.EndEdit())
        {
            DisputeCategory obj = DataSourceDisputeCategory.FindByDisputeCategoryID(SelectedDisputeCategoryID);
            DisputeCategory.Update(obj);

            // TODO: Set Feedback text...

            DisputeCategoryList1.DataBind(DataSourceDisputeCategory);
            SetLayout(LayoutType.View);
            divexistname.Visible = false;
        }
        else
        {
            divexistname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlDisputeCategoryList.Visible = false;
                pnlDisputeCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlDisputeCategoryList.Visible = true;
                pnlDisputeCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion

}
